

#Clear R
rm(list = ls())

#Set working directory
setwd("/Users/patrickhulme/Dropbox/Replicate/War_and_Responsibility")


######################################################WAR VOTES#################################################


###Create DF to Compare War Votes to Competing Proxies of Congressional Sentiment toward use of military force (Figure A8: Accuracy of Competing Measures in Predicting Use of Military Force Votes)
library(doBy)

#GET VOTES FOR WAR VOTES

library(readr)
vote_data <- read_csv("Data_files/Votes_and_Polls/War_votes.csv")

vote_data <- vote_data[c("Bill","Vote (Voteview ID)","percent_favor","crisno","Against or For Force?","congress","House","prez_party_code")]


###FP Ideology Scores (Jeong)
library(readxl)
FP_House <- read_excel("Data_files/Votes_and_Polls/FP_Positions_House_1945_2010.xlsx")
FP_House <- FP_House[-c(5,6)]
JeongHouse <- summaryBy(FP_mean~congress, data=FP_House,FUN=c(mean),na.rm=TRUE,
                        keep.names=TRUE)
JeongHouse$House <- "H"

FP_Senate <- read_excel("Data_files/Votes_and_Polls/FP_Positions_Senate_1945_2010.xlsx")
FP_Senate <- FP_Senate[-c(4)]
JeongSenate <- summaryBy(FP_mean~congress, data=FP_Senate,FUN=c(mean),na.rm=TRUE,
                         keep.names=TRUE)
JeongSenate$House <- "S"
FP <- rbind(JeongHouse,JeongSenate)



###Presidential Support Scores
library(readr)
senate_presidential_support <- read_csv("Data_files/Votes_and_Polls/senate_presidential_support.csv")
Prez_support_Senate <- summaryBy(score~congress, data=senate_presidential_support,FUN=c(mean),na.rm=TRUE,
                                 keep.names=TRUE)
Prez_support_Senate$House <- "S"

library(readr)
house_presidential_support <- read_csv("Data_files/Votes_and_Polls/house_presidential_support.csv")
Prez_support_House <- summaryBy(score~congress, data=house_presidential_support,FUN=c(mean),na.rm=TRUE,
                                keep.names=TRUE)
Prez_support_House$House <- "H"

presidential_support <- rbind(Prez_support_House,Prez_support_Senate)

FP <- merge(FP,presidential_support, all.x = T)

vote_data <- merge(vote_data,FP, by = c("congress","House"), all.x = T)


###DW Nom. Dimensions 1 & 2
library(readr)
voteview_polarization_data <- read_csv("Data_files/Votes_and_Polls/voteview_polarization_data.csv")
voteview_polarization_data <- voteview_polarization_data[c(1,2,9,10)]

voteview_polarization_data$House <- ifelse(voteview_polarization_data$chamber == "House", "H", "S")
vote_data <- merge(vote_data,voteview_polarization_data, by = c("congress", "House"), all.x = T)


###Partisan Composition of Congress (% Dem)
library(readr)
Partisan_breakdown <- read_csv("Data_files/Votes_and_Polls/Partisan_breakdown.csv")

vote_data <- merge(vote_data,Partisan_breakdown, by = c("congress", "House"), all.x = T)


###Partisan Composition of Congress (% Copartisan of Pres.)
vote_data$prez_copartisans <- NA
vote_data$prez_copartisans <- ifelse(vote_data$prez_party_code == "100",vote_data$percent_dem,(1-vote_data$percent_dem))


#need to adjust percent support for whether vote is for of against use of force
vote_data$percent_favor <- as.numeric(vote_data$percent_favor)
vote_data$percent_favor_force <- ifelse(vote_data$`Against or For Force?` == "F", vote_data$percent_favor, (1-vote_data$percent_favor))
vote_data$percent_repub <- 1-vote_data$percent_dem

#dictionary sentiment (sentimentR)
library(readr)

####this takes a long time---for convenience, output provided as csv ("dictionary_sent.csv"). Original code provided in separate file#######

dictionary_sent <- read_csv("Data_files/Votes_and_Polls/dictionary_sent.csv")
vote_data <- merge(vote_data,dictionary_sent, by = "crisno", all.x = T)


rm(list = setdiff(ls(), c("vote_data")))

write.csv(vote_data,"Data_files/vote_data.csv", row.names = FALSE)

rm(list = ls())





######################################################PUBLIC OPINION POLLS###################################################


rm(list = ls())

Polls <- read_csv("Data_files/Votes_and_Polls/raw_polls_data.csv") #crisis data


###FP Ideology Scores (Jeong)
library(readxl)
FP_House <- read_excel("Data_files/Votes_and_Polls/FP_Positions_House_1945_2010.xlsx")
FP_House <- FP_House[-c(5,6)]

library(doBy)
JeongHouse <- summaryBy(FP_mean~congress, data=FP_House,FUN=c(mean),na.rm=TRUE,
                        keep.names=TRUE)
JeongHouse$House <- "H"

library(readxl)
FP_Senate <- read_excel("Data_files/Votes_and_Polls/FP_Positions_Senate_1945_2010.xlsx")
FP_Senate <- FP_Senate[-c(4)]

JeongSenate <- summaryBy(FP_mean~congress, data=FP_Senate,FUN=c(mean),na.rm=TRUE,
                         keep.names=TRUE)
JeongSenate$House <- "S"
FP <- rbind(JeongHouse,JeongSenate)
FP <- doBy::summaryBy(FP_mean ~ congress, FP, FUN = mean)



###Presidential Support Scores
library(readr)
senate_presidential_support <- read_csv("Data_files/Votes_and_Polls/senate_presidential_support.csv")

Prez_support_Senate <- summaryBy(score~congress, data=senate_presidential_support,FUN=c(mean),na.rm=TRUE,
                                 keep.names=TRUE)
Prez_support_Senate$House <- "S"

house_presidential_support <- read_csv("Data_files/Votes_and_Polls/house_presidential_support.csv")
Prez_support_House <- summaryBy(score~congress, data=house_presidential_support,FUN=c(mean),na.rm=TRUE,
                                keep.names=TRUE)
Prez_support_House$House <- "H"

presidential_support <- rbind(Prez_support_House,Prez_support_Senate)
presidential_support <- doBy::summaryBy(score ~ congress, presidential_support, FUN = mean)

FP <- merge(FP,presidential_support, all.x = T)
Polls <- merge(Polls,FP, by = "congress", all.x = T)

###DW Nom. Dimensions 1 & 2
voteview_polarization_data <- read_csv("Data_files/Votes_and_Polls/voteview_polarization_data.csv")

voteview_polarization_data <- voteview_polarization_data[c(1,2,9,10)]

voteview_polarization_data$House <- ifelse(voteview_polarization_data$chamber == "House", "H", "S")

voteview_polarization_data <- doBy::summaryBy(chamber.mean.d1 + chamber.mean.d2 ~ congress, voteview_polarization_data, FUN = mean)

Polls <- merge(Polls,voteview_polarization_data, by = c("congress"), all.x = T)


###Partisan Composition of Congress (% Dem)
library(readr)
Partisan_breakdown <- read_csv("Data_files/Votes_and_Polls/Partisan_breakdown.csv")

Partisan_breakdown <- doBy::summaryBy(percent_dem ~ congress, Partisan_breakdown, FUN = mean)

Polls <- merge(Polls,Partisan_breakdown, by = c("congress"), all.x = T)


###Partisan Composition of Congress (% Copartisan of Pres.)
Polls$prez_copartisans <- NA
Polls$prez_copartisans <- ifelse(Polls$Prez_party == "100",Polls$percent_dem,(1-Polls$percent_dem))
Polls$percent_repub <- 1-Polls$percent_dem


#dictionary sentiment (sentimentR)
library(readr)
dictionary_sent <- read_csv("Data_files/Votes_and_Polls/dictionary_sent.csv")

Polls <- merge(Polls,dictionary_sent, by = "crisno", all.x = T)

Polls$Pub_supp <- Polls$Support/(Polls$Support + Polls$Oppose)

rm(list = setdiff(ls(), c("Polls")))

write.csv(Polls,"Data_files/polls_data.csv", row.names = FALSE)

rm(list = ls())
